package com.palnar.ilecker.model;

import java.io.Serializable;

import org.ksoap2.serialization.SoapObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.palnar.ilecker.utils.XmlDocumentUtility;

public class User implements Serializable {

	private Integer id = 0;
	private String name = "";
	private String username = "";
	private String password = "";
	private Role role;
	private byte[] imageData;

	private static String idArg = "Id";
	private static String nameArg = "Name";
	private static String usernameArg = "UserName";
	private static String passwordArg = "Password";
	private static String roleArg = "Role";
	private static String imageDataArg = "ImageData";

	public User() {
		role = new Role();
		imageData = new byte[20];
	}

	public Integer GetId() {
		return this.id;
	}

	public void SetId(Integer id) {

		System.out.println("id" + id);
		this.id = id;
	}

	public String GetName() {
		return this.name;
	}

	public void SetName(String name) {
		System.out.println("name " + name);

		this.name = name;
	}

	public String GetUserName() {
		return this.username;

	}

	public void SetUserName(String userName) {

		System.out.println("username " + userName);
		this.username = userName;
	}

	public String GetPassword() {
		return this.password;
	}

	public void SetPassword(String password) {
		System.out.println("password " + password);
		this.password = password;
	}

	public Role GetRole() {
		return this.role;
	}

	public void SetRole(Role role) {
		this.role = role;
	}

	public byte[] GetImageData() {
		return this.imageData;
	}

	public void SetImageData(byte[] imageData) {
		this.imageData = imageData;
	}

	public void getSerializationData(Element parentElement, Document document) {

		Element idElement = document.createElement(idArg);
		Node idNode = document.createTextNode("id");
		idNode.setNodeValue(id.toString());
		idElement.appendChild(idNode);
		parentElement.appendChild(idElement);

		Element nameElement = document.createElement(nameArg);
		Node nameNode = document.createTextNode("name");
		nameNode.setNodeValue(name);
		nameElement.appendChild(nameNode);
		parentElement.appendChild(nameElement);

		Element usernameElement = document.createElement(usernameArg);
		Node usernameNode = document.createTextNode("username");
		usernameNode.setNodeValue(username);
		usernameElement.appendChild(usernameNode);
		parentElement.appendChild(usernameElement);

		Element passwordElement = document.createElement(passwordArg);
		Node passwordNode = document.createTextNode("password");
		passwordNode.setNodeValue(password);
		passwordElement.appendChild(passwordNode);
		parentElement.appendChild(passwordElement);

		Element roleElement = document.createElement(roleArg);
		this.role.getSerializationData(roleElement, document);
		parentElement.appendChild(roleElement);

		if (imageData != null) {
			Element imageDataElement = document.createElement(imageDataArg);
			Node imageDataNode = document.createTextNode("imageData");
			imageDataNode.setNodeValue(imageData.toString());
			imageDataElement.appendChild(imageDataNode);
			parentElement.appendChild(imageDataElement);
		}

	}

	public static User UserTypeBuilder(SoapObject object) {

		User user = new User();

		Object idObj = object.getProperty(idArg);
		if (!idObj.toString().equals(XmlDocumentUtility.nullString)) {
			user.SetId(Integer.parseInt(idObj.toString()));
		}

		Object nameObj = object.getProperty(nameArg);
		if (nameObj != null) {
			if (!nameObj.toString().equals(XmlDocumentUtility.nullString)) {
				user.SetName(nameObj.toString());
			}
		}

		Object userNameObj = object.getProperty(usernameArg);
		if (userNameObj != null) {
			if (!userNameObj.toString().equals(XmlDocumentUtility.nullString)) {
				user.SetUserName(userNameObj.toString());
			}
		}

		Object passwordObj = object.getProperty(passwordArg);
		if (passwordObj != null) {
			if (!passwordObj.toString().equals(XmlDocumentUtility.nullString)) {
				user.SetPassword(object.getProperty(passwordArg).toString());
			}
		}
		SoapObject roleObject = (SoapObject) object.getProperty(roleArg);
		if (roleObject != null) {
			user.SetRole(Role.RoleTypeBuilder(roleObject));
		}

		user.SetImageData(null);

		return user;
	}

}
